﻿; (function () {
    var schedule = new Schedule("InPatient", $('#hdnRelativeUrl').val());
    schedule.days = 1;
    var outPatient = null;

    var page = {
        init: function () {
            page.commonUrl = $('#hdnCommonUrl').val();
            page.getPointOfCare(page.patientArea);

            pageEvent.init();
        },

        getPointOfCare: function () {
            $.post(page.commonUrl + '/' + 'GetDept?deptType=PointOfCare', '', function (data) {
                var temp = '';
                $.each(data, function (i, n) {
                    if (n.selected) {
                        temp += ' <option value="' + n.id + '" selected>' + n.text + '</option>';
                    } else {
                        temp += ' <option value="' + n.id + '">' + n.text + '</option>';
                    }
                });
                $('#pointOfCare').append(temp);
            });
        }
    };

    var pageEvent = {
        init: function () {
            //选择时间
            $('.inpatient_main').on('click', '.chose_time,.inpatient_one', function () {
                var $this = $(this);
                var medRecNO = $this.data('medrecno');
                schedule.queryCondition.MedRecNO = medRecNO;

                if(!outPatient) {
                    outPatient = new OutPatient(schedule, {
                        showBack:true, 
                        thisContainer: 'outPatient', 
                        backContainer:'inPatient', 
                        backCallback: function(){
                            $.each(schedule.patientList, function(i, n){
                                if(n.MedRecNO == medRecNO){
                                    n.ExamRequestList = schedule.examRequestList;
                                    $('#'+ medRecNO).html(pageEvent.appendPatientInfo(n));
                                    $('.list_main').height(243);

                                    return false;
                                }
                            });
                        }});
                    outPatient.init();
                } else {
                    outPatient.init(true);
                }

                outPatient.clear();
                $('#outPatient').show();
                $('#inPatient').hide();
            });

            //查询
            $('#searchBtn').click(function () {
                var pointOfCare = $('#pointOfCare option:selected').text();
                var bed = $('#patientBed').val();
                var name = $('#patientName').val();
                var inPatientNO = $('#patientNum').val();

                schedule.queryCondition = {
                    "PointOfCare": pointOfCare,
                    "InPatientNO": inPatientNO,
                    "Name": name,
                    "Bed": bed,
                    "RequestedDateBegin": $('#applyTimeStart').val(),
                    "RequestedDateEnd": $('#applyTimeEnd').val()
                };
                schedule.getPatientExamRequestList(pageEvent.getPatientExamRequestListCallback);

                $('.inpatient_main').html('');
            });
        },

        getPatientExamRequestListCallback: function (data) {
            $.each(data, function (i, n) {
                var bed = !n.Bed ? '' : n.Bed+'床';

                var temp = '';
                temp += '<div id="'+ n.MedRecNO +'" class="inpatient_item">' + 
                            pageEvent.appendPatientInfo(n);
                        '</div>';

                $('.inpatient_main').append(temp);
            });

            $('.inpatient_main').height($(window).height() - 160);
            $('.list_main').height(243);
        },

        appendPatientInfo : function(patient){
            var bed = !patient.Bed ? '' : patient.Bed;

            var html = '<div class="inpatient_list ">' +
                            '<div class="list_top">' +
                                '<span>' + patient.Name + '(' + patient.ExamRequestList.length + ')' + '</span>' +
                                '<span class="patient_num">' + bed + '床</span>' +
                            '</div> ' +
                            '<div class="list_main">';

            $.each(patient.ExamRequestList,function(i,n){
                var border_bottom = '';
                if (i == (patient.ExamRequestList.length-1) && i>2) {
                    border_bottom = 'border_bottom';
                }

                var has_finish = '';
                if (n.ScheduleFlag) { 
                    has_finish = 'has_finish' 
                }

                html +='<div data-placerorderno=' + n.PlacerOrderNO + ' class="inpatient_one ' + has_finish + ' ' + border_bottom + '">' +
                            '<input class="inpatient_input" value="" type="hidden"/>' +
                            '<div class="inpatient_details">' +
                                '<span class="sectid">' + n.ServiceSectID + '</span>' +
                                '<span class="time">' + n.ProcedureName + '</span>' +
                            '</div>';

                if(n.ScheduleFlag){
                    var scheduleTime = n.ScheduleStartDate.substr(11, 5) + "-" + n.ScheduleEndDate.substr(11, 5);
                    html += '<div class="inpatient_time">预约时间：' + 
                                n.ScheduleStartDate.substr(0, 10) + '   ' + scheduleTime +
                            '</div>';
                } else {
                    html += '<div class="inpatient_time">申请时间：' + 
                                n.RequestedDate+
                            '</div>';
                }

                html += '</div>';
            });

            html +='</div>' +
                    '<div class="chose_time" data-medRecNO="'+ patient.MedRecNO +'">选择时间</div>' + 
                    '</div></div>';

            return html;
        }
    }

    page.init();

})();